import numpy as np
import itertools
import math

class BaseStatFromFieldCollection(object):

    def _CalcStatFromList(self, valList, statOp):
        if statOp == "mean":
            npArr = np.array(valList)
            return float(np.mean(npArr))
        elif statOp == "std":
            npArr = np.array(valList)
            return float(np.std(npArr))
        elif statOp == "ste":
            npArr = np.array(valList)
            return float(np.std(npArr)) / math.sqrt(len(valList))
        elif statOp == "distribution":
            distDict = {}
            
            for val in valList:
                if val in distDict.keys():
                    distDict[val] += 1
                else:
                    distDict[val] = 1
            
            return distDict
        elif statOp == "max":
            return max(valList)
    
        elif statOp == "min":
            return min(valList)
        
        elif statOp == "list":
            return valList
        
        elif statOp == "flat":
            #Takes a list of lists and flattens it to a list.
            return list(itertools.chain.from_iterable(valList))
    
        else:
            print("Unknown stat operation: " + statOp)
            return None
            